<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\exception\UploadException;
use app\common\library\Upload;
use app\common\model\Area;
use app\common\model\Version;
use fast\Random;
use think\captcha\Captcha;
use think\Config;
use think\Db;
use think\Hook;

/**
 * 公共接口
 */
class Boss extends Api
{
    protected $noNeedLogin = [''];
    protected $noNeedRight = '*';


    /* 全部区域 */
    public function getallquyu(){


        $list=Db::name('aa_quyu')->select();

        $this->success('全部区域',$list);

    }

    /* 公司数据  */

    public function getallcompany(){

        $id=$this->request->param('id');

        $map = array();
        if ($id){
            $map['quyu_id'] = [ 'eq',$id];
        }

        $data['data1']=Db::name('aa_client')
            ->where($map)
            ->count();

        $data['data5']=Db::name('aa_client')
            ->where($map)
            ->where('types','eq',1)
            ->count();

        $data['data6']=Db::name('aa_client')
            ->where($map)
            ->where('types','eq',0)
            ->count();

        $data['data7']=Db::name('aa_client')
            ->where($map)
            ->where('ctypes','eq',3)
            ->count();


        $this->success('全部区域',$data);

    }


    /* 今日数据 */
    public function gettodydata(){

        $strtime=$this->request->param('statrtime');
        $endtime=$this->request->param('endtime');

        $strtimes=strtotime($strtime);
        $endtimes=strtotime($endtime)+86400;



        $map['time'] = ['between' , "$strtimes,$strtimes"];
        $data['data1']=Db::name('client_log')
            ->where($map)
            ->count();

        $data['data2']=Db::name('aa_client')
            ->where($map)
            ->count();


        $maps['startime'] = ['gt',$strtimes];
        $maps['endtime'] = ['lt',$strtimes];

        $hour=Db::name('workhour')
            ->where($maps)
            ->sum('hour');
        $quyu=Db::name('aa_quyu')->count();

        if ($hour){
            $data['data3']=round(($hour/$quyu),2);
        }else{
            $data['data3']=0;
        }

        $data['data4']=Db::name('aa_client')
            ->where($map)
            ->count();


        $map2['ctypestime'] = ['between' , "$strtimes,$strtimes"];
        $data['data5']=Db::name('aa_client')
            ->where($map2)
            ->where('ctypes','eq',1)
            ->count();


        $map2['ctypestime'] = ['between' , "$strtimes,$strtimes"];
        $data['data5']=Db::name('aa_client')
            ->where($map2)
            ->where('ctypes','eq',3)
            ->count();


        $this->success('全部区域',$data);
    }


    // 拜访排行
    public function lists1(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $times=strtotime(date('Y-m-d'));


        $set=Db::name('work_set')->find();

        $ds=$set['number1'];



        foreach ($quyu as &$key){

            $count=Db::name('client_log')
                ->where('quyu_id','eq',$key['id'])
                ->where('time','gt',$times)
                ->where('types','eq',1)
                ->count();

            $key['number']=$count;

            $bili=0;
            if ($count>0){
                $bili=round( $count/$ds,2);
            }

            $key['bili']=$bili;

        }
        // 调用冒泡排序函数
        $quyu=  bubbleSort($quyu);

        $this->success('本月拜访排行',$quyu);


    }



    // 新增客户
    public function lists2(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $times=strtotime(date('Y-m-d'));

        foreach ($quyu as &$key){

            $count=Db::name('aa_client')
                ->where('quyu_id','eq',$key['id'])
                ->where('time','gt',$times)
                ->count();

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }



    // 工作时常
    public function lists3(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $times=strtotime(date('Y-m-d'));

        foreach ($quyu as &$key){

            $count=Db::name('workhour')
                ->where('quyu_id','eq',$key['id'])
                ->where('startime','gt',$times)
                ->sum('hour');

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }

    // 新增合作
    public function lists4(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $times=strtotime(date('Y-m-d'));

        foreach ($quyu as &$key){

            $count=Db::name('aa_client')
                ->where('quyu_id','eq',$key['id'])
                ->where('ctypestime','gt',$times)
                ->where('ctypes','eq',1)
                ->count();

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }


    // 新增掉客
    public function lists5(){

        $quyu=Db::name('aa_quyu')
            ->alias('a')
            ->join('fa_user b','a.user_id=b.id')
            ->field('a.*,b.nickname,b.avatar')
            ->select();

        $times=strtotime(date('Y-m-d'));

        foreach ($quyu as &$key){

            $count=Db::name('aa_client')
                ->where('quyu_id','eq',$key['id'])
                ->where('ctypestime','gt',$times)
                ->where('ctypes','eq',3)
                ->count();

            $key['number']=$count;
        }
        // 调用冒泡排序函数
        $res=  bubbleSort($quyu);

        $this->success('本月新增客户',$quyu);


    }













}

